/* Class Name: OORTelesalesLeadTrigHandler
 * Author: Acceture
 * Date: 3-May-2011
 * Requirement/Project Name: OOR Telesales
 * Requirement Description: Methods invoked from lead  trigger 
 * Bhumika Udani    12 May 2012     v1.1            Included de-dup campaign member logic as per 1492
 **/
 
public class OORTelesalesTriggerHandler{

    /*
     *  Update the Time zone with associated zip code.
     *
     **/
    public static void updateTimeZone(List<Lead> leadList , Set<String> zipCodeSet ){
        // Map store and map the zipcode and time zone.
        final Map<String,String> mapZipCodeAndTimeZone  = new Map<String,String>();
        
        // Check if any item is present in the list 
        if(leadList != null && leadList.size() > 0){            
            for(ZipCode__c zipCodeObj : [Select Zip_Code__c,Time_Zone__c from ZipCode__c where  Zip_Code__c in :zipCodeSet] ){
            
                mapZipCodeAndTimeZone.put(zipCodeObj.Zip_Code__c,zipCodeObj.Time_Zone__c);
            }
            
            for(Lead leadObj : leadList){
               leadObj.Time_Zone__c = mapZipCodeAndTimeZone.get(leadObj.PostalCode);
            }
        }
    }
    //Below method is called from OORTelesalesAssociateCampaignOnLead trigger campaign member updates
    public static void duplicateLeadCampaignMemberCheck(set<CampaignMember> camMemList ){

          //Declaring collections to be used
          
          list<lead> toUpdateLeadList =  new list<lead>();        
          Lead leadNew = null;         
         
          for(campaignMember dupCheckMem : camMemList){
            
                       leadNew = new Lead(Id=dupCheckMem.LeadId);
                       leadNew.OORTelesalesCampaign__c=dupCheckMem.CampaignId;
                            if(dupCheckMem.CampaignActive__c == 'true'){
                                    leadNew.ownerid = OORTelesalesUtility.getQueueID(OORTelesalesTextConstant.OOR_TELE_CAMPAIGN_LEAD_QUEUE);    
                            	}
                       toUpdateLeadList.add(leadNew); 
                   }
           
        // Exception handling
          try{
              if(toUpdateLeadList.size()>0){                    
                  	update toUpdateLeadList;   
                 	system.debug('toUpdateLeadList' + toUpdateLeadList);  
              } 
              
         }
         
         catch(exception campaignMemExcep){
             GeneralConstants.mailAnyExceptionToMe('OORTelesalesAssociateCampaignOnLead  TRIGGER','OORTelesalesAssociateCampaignOnLead ',campaignMemExcep);   
         }          
    }
    
    /*
     *  Update the Lead Status on Campaign member 
     *
     **/
    public static void updateLeadStatusOnCampaignMember(Set<Id> leadIdSet,Set<ID> campIdSet,Map<ID,Lead> newLeadMap)
    {
        final List<CampaignMember>  campMemberList = new List<CampaignMember>(); 
        
        // Check if any element is present.
        
        if(leadIdSet.size() > 0 && campIdSet.size() > 0)
        {
            for ( CampaignMember campMemberObj : [Select LeadStatusCheck__c, LeadID from CampaignMember where CampaignID in :campIdSet  and  LeadID in:leadIdSet] ) 
            {
                campMemberObj.LeadStatusCheck__c = newLeadMap.get(campMemberObj.LeadID).Lead_Status_ReadOnly__c;
                campMemberList.add(campMemberObj);
            }
        }
        // update if any element is present 
        if(campMemberList.size() > 0)
        {
            update campMemberList;
        }
        
    
    }
    
    
}